######################################################################
##                                                                  ## 
##      PROJECT TITLE:    SHOW ME THE MONEY                         ##
##      PROJECT AUTHOR:   THOMAS S. ROBINSON                        ##
##      EMAIL:            THOMAS.ROBINSON@POLITICS.OX.AC.UK         ##
##                                                                  ##
##      DESCRIPTION:      CLEAN DATA AND TRANSFORM FOR ANALYSIS     ##
##                                                                  ##
######################################################################

#### Packages ####
# devtools::install_github("ropensci/qualtRics")

library(tidyverse) #v1.3.1
library(qualtRics) #v3.1.5
library(miceadds) #v3.11-6
library(Hmisc) #v4.6-0

relevel_factors <- function(df) {
  
  df$average <- factor(df$average, levels = c("$10,000","$75","$1 million"))
  df$largest <- factor(df$largest, levels = c("Private individual",
                                              unique(df$largest[df$largest != "Private individual"])))
  df$prop <- factor(df$prop, levels = c("50%",
                                unique(df$prop[df$prop != "50%"])))
  
  return(df)
}

cluster_glm <- function(data, formula, cluster, type) {
    
    mod <- glm.cluster(formula = formula,
                       data = data,
                       cluster = cluster,
                       weights = NULL,
                       family = if(type == "logit") {
                         binomial(link="logit")
                         } else { 
                           gaussian
                           }
                       ) %>%
      summary(.) %>%
      as.data.frame(.) %>%
      mutate(coef_name = rownames(.),
             ci = 1.96*`Std. Error`,
             sig = if(type == "logit") {ifelse(`Pr(>|z|)` < 0.001,"***",
                          ifelse(`Pr(>|z|)` < 0.01, "**",
                                 ifelse(`Pr(>|z|)` < 0.05,"*","")))}
                   else {ifelse(`Pr(>|t|)` < 0.001,"***",
                                ifelse(`Pr(>|t|)` < 0.01, "**",
                                       ifelse(`Pr(>|t|)` < 0.05,"*","")))}) %>%
      mutate(attribute = ifelse(grepl("average",coef_name),"Average Donation",
                          ifelse(grepl("total",coef_name),"Total Donations",
                          ifelse(grepl("largest",coef_name),"Largest Donor",
                          ifelse(grepl("prop",coef_name),"Proportion by Largest Donor",
                          ifelse(grepl("origin",coef_name),"Origin of Donations",
                          ifelse(grepl("party",coef_name),"Party",
                          ifelse(grepl("cand_ideology2",coef_name),"Ideological Distance",
                          ifelse(grepl("cand_ideology",coef_name),"Candidate Ideology",
                          ifelse(grepl("office",coef_name),"Previous Office",
                          ifelse(grepl("issue",coef_name),"Issue",NA))))))))))) %>%
    
      mutate(coef_name = gsub("cand_ideology2","Ideological Distance",coef_name),
             coef_name = gsub("partySame","Party: Same",coef_name),
             coef_name = gsub("partyDifferent","Party: Different",coef_name),
             
             coef_name = gsub("average","Average: ",coef_name),
             coef_name = gsub("total","Total: ",coef_name),
             coef_name = gsub("largest","Largest: ",coef_name),
             coef_name = gsub("prop","Proportion: ",coef_name),
             coef_name = gsub("origin","",coef_name),
             coef_name = gsub("party","",coef_name),
             coef_name = gsub("office","",coef_name),
             coef_name = gsub("cand_ideology","",coef_name),
             coef_name = gsub("issueenviro","Issue: Carbon Emissions Tax",coef_name),
             coef_name = gsub("issuemarij","Issue: Marijuana Legalisation",coef_name),
             coef_name = gsub("issuewage","Issue: Minimum Wage Increase",coef_name),
             coef_name = gsub("round","Round",coef_name)) %>%
      
      add_row(Estimate = 0,
              attribute = c("Average Donation","Total Donations", "Largest Donor",
                            "Proportion by Largest Donor","Origin of Donations"),
              coef_name = c("Average: $10,000","Total: $1 million to $10 million",
                            "Largest: Private individual","Proportion: 50%",
                            "Majority from donors out of state")) %>% 
      
      mutate(coef_name = factor(coef_name,
                                levels = c("Issue: Carbon Emissions Tax",
                                           "Issue: Marijuana Legalisation",
                                           "Issue: Minimum Wage Increase",
                                           "Average: $75",
                                           "Average: $10,000",
                                           "Average: $1 million",                           
                                           "Total: $100,000 to $200,000",
                                           "Total: $1 million to $10 million",
                                           "Total: $70 million to $90 million",
                                           "Largest: Private individual",
                                           "Largest: Identity not disclosed",               
                                           "Largest: Labor Union",                          
                                           "Largest: Corporation/Trade Association",        
                                           "Largest: Political Advocacy Group",             
                                           "Proportion: 10%",
                                           "Proportion: 50%",
                                           "Proportion: 90%",
                                           "Majority from donors out of state",
                                           "Majority from donors within the state",
                                           "Independent",                          
                                           "Republican",
                                           "Very conservative", 
                                           "Moderate conservative",                
                                           "Moderate liberal",                     
                                           "Very liberal",
                                           "Party: Same",
                                           "Party: Different",
                                           "Ideological Distance",
                                           "Elected to state ",                    
                                           "No previous elected positions",
                                           "Round"))
             )
      
    return(mod)
      
}

cj_plot <- function(mod, multi = FALSE, x_lab = "AMCE") {
  
  mod <- filter(mod, coef_name != "(Intercept)")
  
  if (!(multi == FALSE)) {
      
    ggplot(mod, aes_string(x = "Estimate", y = "coef_name", color = multi)) +
        geom_vline(xintercept = 0, linetype = "dashed") +
        geom_point(position = position_dodge(.5)) +
        geom_errorbarh(height = 0.1, position = position_dodge(.5),
                       aes(xmin = Estimate - ci, xmax = Estimate + ci)) + 
        theme_minimal() +
        labs(x = x_lab, y = "Attribute-level", color = capitalize(multi))
        
      } else {
        
      ggplot(mod, aes(x = Estimate, y = coef_name)) +
        geom_vline(xintercept = 0, linetype = "dashed") +
        geom_point() +
        geom_errorbarh(height = 0.1, aes(xmin = Estimate - ci, xmax = Estimate + ci)) + 
        theme_minimal() +
        labs(x = x_lab, y = "Attribute-level")
  }
}

bold_text_wrapper <- function(string) {
  
  paste0("\\textbf{",string,"}")
  
}

# Basic conjoint function to pass via map
init_vote_cj <- function(df) {
  cluster_glm(formula = vote ~ average + total + largest + prop + origin,
              data = df, type = "ols",
              cluster = "participantid")
}